package dianmobile.eistarandroid.ui.fragments;


import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import dianmobile.eistarandroid.R;
import dianmobile.eistarandroid.network.request.DelFriendsRequest;
import dianmobile.eistarandroid.network.request.MyFriendsRequest;
import dianmobile.eistarandroid.network.request.RequestCallback;
import dianmobile.eistarandroid.network.request.RequestType;
import dianmobile.eistarandroid.ui.Adapter.FriendsAdapter;
import dianmobile.eistarandroid.ui.activities.UserInfoActivity;
import dianmobile.eistarandroid.utils.ConstantsData;
import zrc.widget.SimpleHeader;
import zrc.widget.ZrcListView;

public class MyFriendsFragment extends Fragment {

    private ZrcListView listView;
    private List<String> listData = new ArrayList<>();
    private FriendsAdapter adapter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_my_friends, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        listView = (ZrcListView)getActivity().findViewById(R.id.lv_friends_list);

        setPullToRefresh();

        adapter = new FriendsAdapter(getActivity(), listData);
        listView.setAdapter(adapter);
        getFriendsData();

        listView.setOnItemClickListener(new ZrcListView.OnItemClickListener() {
            @Override
            public void onItemClick(ZrcListView parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), UserInfoActivity.class);
                intent.putExtra(ConstantsData.EXTRA_USER_NAME, listData.get(position));
                startActivity(intent);
            }
        });
        listView.setOnItemLongClickListener(new ZrcListView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(ZrcListView parent, View view, int position, long id) {
                deleteDialog(listData.get(position));
                return true;
            }
        });


    }

    public void getFriendsData() {

        //final ProgressDialog progressDialog = new ProgressDialog(getActivity());
        //progressDialog.setTitle("加载中...");
        //progressDialog.show();

        MyFriendsRequest.execute(getActivity(), new RequestCallback() {
            @Override
            public void onResult(Map<String, Object> result, String error) {
                if (error != null) {
                    //progressDialog.dismiss();
                    //Toast.makeText(getActivity(), "network error", Toast.LENGTH_SHORT).show();
                    listView.setRefreshFail("网络错误");
                } else {
                    //progressDialog.dismiss();
                    listData.clear();
                    listData.addAll((ArrayList<String>) result.get(ConstantsData.MY_FRIENDS));
                    adapter.notifyDataSetChanged();
                    listView.setRefreshSuccess();
                }

            }
        });
    }


    /**
     * config the pull-to-refresh
     *
     */
    private void setPullToRefresh() {
        // 设置下拉刷新的样式（可选，但如果没有Header则无法下拉刷新）
        SimpleHeader header = new SimpleHeader(getActivity());
        header.setTextColor(0xff0066aa);
        header.setCircleColor(0xff33bbee);
        listView.setHeadable(header);


        // 下拉刷新事件回调
        listView.setOnRefreshStartListener(new ZrcListView.OnStartListener() {
            @Override
            public void onStart() {
                getFriendsData();
            }
        });
    }

    private void deleteDialog(final String friendsName) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("提示");
        builder.setMessage("确定删除吗");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Map<String, String> params = new HashMap<>();
                params.put("userid", friendsName);
                params.put("xml", "1");
                DelFriendsRequest.execute(getActivity(), RequestType.DEL_FRIENDS, params, new RequestCallback() {
                    @Override
                    public void onResult(Map<String, Object> result, String error) {
                        //TODO 解析HTML
                        if (error != null) {
                            Toast.makeText(getActivity(), "网络错误", Toast.LENGTH_SHORT).show();
                        } else {
                            if (result.containsKey(ConstantsData.ERROR_RESULT)) {
                                Toast.makeText(getActivity(), (String) result.get(ConstantsData.ERROR_RESULT), Toast.LENGTH_SHORT).show();
                            } else {
                                listData.remove(friendsName);
                                adapter.notifyDataSetChanged();
                            }
                        }
                    }
                });

                dialog.dismiss();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }
}
